/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.ds.MockDS;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.User;
import org.cx1.service.api.IAuthService;

/**
 *
 * @author ECH
 */
public class MAuthService implements IAuthService {

    @Override
    public ServiceResponse<Boolean> authenticate(String email, String password) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {
            List<User> users = MockDS.getUsers();
            for (User user : users) {
                if (StringUtils.equalsIgnoreCase(email, user.getEmail()) &&
                    StringUtils.equalsIgnoreCase(password, user.getPassword())) {
                    success = true;
                    break;
                }
            }
            
            if (!success) {
                if (StringUtils.equalsIgnoreCase(email, "ham") &&
                    StringUtils.equalsIgnoreCase(password, "day")) {
                    success = true;
                }
            }
            
            response.setResponse(success);
            response.setSuccessful(true);            
        } catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }
    
    @Override
    public ServiceResponse<String> authenticate2(String email, String password) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String userID = null;
        try {
            List<User> users = MockDS.getUsers();
            for (User user : users) {
                if (StringUtils.equalsIgnoreCase(email, user.getEmail()) &&
                    StringUtils.equalsIgnoreCase(password, user.getPassword())) {
                    userID = user.getId();
                    break;
                }
            }
                        
            if (StringUtils.isEmpty(userID)) {
                if (StringUtils.equalsIgnoreCase(email, "ham") &&
                    StringUtils.equalsIgnoreCase(password, "day")) {
                    userID = "xxxxx-xxxxx-xxxxx-xxxxx";
                }
            }
            
            response.setResponse(userID);
            response.setSuccessful(true);            
        } catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }
}
